'use strict';
/**
 * 吃比组件
 */
(function (exports) {
    var CardSelect = cc.Layer.extendEX({
        ctor: function (_parent) {
            this._super();
            var that = this;
            this.viewParent = _parent;
            this.img = new cc.Sprite(zipai.res.chi_bg1);
            this.img.setPosition(cc.p(0, 120));
            this.img.setScaleX(1.25);
            this.img.setScaleY(1.45);
            this.addChild(this.img);
            TouchUtil.setOnclickListener(this.img, function () {
                if (that.data.length == 3) {
                    that.viewParent.sendChi(that.cards);
                } else {
                    that.setSelect(true);
                    that.viewParent.showBiCard(that.level + 1, that.data[3], that.cards, that.index);
                }
            });

            this.c1 = new Card();
            this.c2 = new Card();
            this.c3 = new Card();
            this.addChild(this.c1);
            this.addChild(this.c2);
            this.addChild(this.c3);
            this['c1'].setScale(0.7);
            this['c2'].setScale(0.7);
            this['c3'].setScale(0.7);
            this['c1'].setPosition(cc.p(0, 200));
            this['c2'].setPosition(cc.p(0, 120));
            this['c3'].setPosition(cc.p(0, 40));

            return this;
        },
        setData: function (eatCard, data, parent, level, cards, index) {
            //排序  吃的牌放在第一位
            for (var i = 0; i < data.length; i++) {
                if (eatCard == data[i]) {
                    var tmp = data[0];
                    data[0] = data[i];
                    data[i] = tmp;
                    break;
                }
            }
            this.index = index;
            this.viewParent = parent;
            this.eatCard = eatCard;
            this.data = data;
            this.level = level;
            this.cards = mCard.copyCards(cards);
            var isAdded = false;
            for (var i = 0; i < 3; i++) {
                var card = data[i];
                this['c' + (i + 1)].setData(card);
                this['c' + (i + 1)].setLocalZOrder(i + 4);
                if (!isAdded) {
                    if (card == eatCard) {
                        isAdded = true;
                        this['c' + (i + 1)].setChiCard();
                    }
                }
                this.cards.push(card);
            }
        },
        setChiData: function (eatCard, data) {
            //排序  吃的牌放在第一位
            for (var i = 0; i < data.length; i++) {
                if (eatCard == data[i]) {
                    var tmp = data[0];
                    data[0] = data[i];
                    data[i] = tmp;
                    break;
                }
            }
            this.eatCard = eatCard;
            this.data = data;
            for (var i = 0; i < 3; i++) {
                var card = data[i];
                this['c' + (i + 1)].setData(card, this);
            }
        },
        setSelect: function (flag) {
            this.img.setTexture((flag) ? (zipai.res.chi_bg2) : (zipai.res.chi_bg1));
            // this.img.setOpacity(flag ? 250 : 50);
        }
    });
    exports.CardSelect = CardSelect;
})(window);